package xiaohuo.fancyring.data

import org.bukkit.entity.Player
import taboolib.library.configuration.ConfigurationSection
import taboolib.module.configuration.util.getStringColored
import taboolib.module.configuration.util.getStringListColored
import taboolib.platform.compat.getBalance
import taboolib.platform.compat.withdrawBalance
import xiaohuo.fancyring.util.getHealthPercent

class RealBodyData(config: ConfigurationSection) {
    val match = config.getStringColored("Match")!!
    val customName = config.getStringColored("CustomName")!!
    val time = config.getInt("Time")
    val coolDown = config.getLong("CoolDown")
    val stats = config.getStringList("Stats")
    val costVault = config.getDouble("Cost.Vault")
    val costHealth = config.getDouble("Cost.Health") / 100
    val secondCostVault = config.getDouble("SecondCost.Vault")
    val secondCostHealth = config.getDouble("SecondCost.Health") / 100
    val execute = config.getStringListColored("Execute")
    val endExecute = config.getStringColored("EndExecute")

    fun handleCost(target: Player): Boolean {
        if (costVault > target.getBalance()) {
            return false
        }
        val health = target.getHealthPercent(costHealth)
        if (target.health < health) {
            return false
        }
        target.withdrawBalance(costVault)
        target.health -= health
        return true
    }

    fun handleSecondCost(target: Player): Boolean {
        if (secondCostVault > target.getBalance()) {
            return false
        }
        val health = target.getHealthPercent(secondCostHealth)
        if (target.health < health) {
            return false
        }
        target.withdrawBalance(secondCostVault)
        target.health -= health
        return true
    }
}